<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="150px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="AccessKey ID" prop="accessKeyId">
						<el-input v-model="form.accessKeyId" placeholder="请输入AccessKey ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="Access Key Secret" prop="accessKeySecret">
						<el-input v-model="form.accessKeySecret" placeholder="请输入Access Key Secret" />
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="SDK App Id" prop="sdkAppId">
						<el-input v-model="form.sdkAppId" placeholder="请输入SDKAppId" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="签名名称" prop="signatureName">
						<el-input v-model="form.signatureName" placeholder="请输入签名名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="模版code" prop="templateCode">
						<el-input v-model="form.templateCode" placeholder="请输入模版code" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="适用类型" prop="applicableType">
						<el-select v-model="form.applicableType" @change="handleChangeType">
							<el-option :value="10" label="应用于指定企业"></el-option>
							<el-option :value="20" label="应用于全平台"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="服务商" prop="supplier">
						<el-select v-model="form.supplier">
							<el-option value="alibaba" label="阿里云"></el-option>
							<el-option value="tencent" label="腾讯云"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20" v-if="form.applicableType === 10">
					<el-form-item label="应用企业" prop="applicationEnterpriseId">
						<el-select v-model="form.applicationEnterpriseId" placeholder="请选择应用企业" @change="handleChangeCompany">
							<el-option v-for="item in siteNameOptions" :key="item.value" :value="item.value" :label="item.label"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="BasicSmsConfigDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/platform/basicSmsConfig';
import { rule } from '/@/utils/validate';
import { fetchList } from '/@/api/platform/airSite';

const emit = defineEmits(['refresh']);
const siteNameOptions = ref([]);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	accessKeyId: '',
	accessKeySecret: '',
	signatureName: '',
	templateCode: '',
	applicableType: 10,
	applicationEnterprise: null,
	applicationEnterpriseId: null,
	supplier: null,
	sdkAppId: '',
});

// 定义校验规则
const dataRules = ref({
	accessKeyId: [{ required: true, message: '请输入', trigger: 'blur' }],
	accessKeySecret: [{ required: true, message: '请输入', trigger: 'blur' }],
	sdkAppId: [{ required: true, message: '请输入', trigger: 'blur' }],
	signatureName: [{ required: true, message: '请输入', trigger: 'blur' }],
	templateCode: [{ required: true, message: '请输入', trigger: 'blur' }],
	applicableType: [{ required: true, message: '请选择', trigger: 'change' }],
	applicationEnterpriseId: [{ required: true, message: '请选择', trigger: 'change' }],
	supplier: [{ required: true, message: '请选择', trigger: 'change' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';
	form.applicationEnterprise = null;
	form.applicationEnterpriseId = null;

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取basicSmsConfig信息
	if (id) {
		form.id = id;
		getbasicSmsConfigData(id);
	} else {
		getSiteNameOptions();
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbasicSmsConfigData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
			getSiteNameOptions();
		})
		.finally(() => {
			loading.value = false;
		});
};

// 获取站点信息
const getSiteNameOptions = () => {
	fetchList({ tag: '1111111111111111111' }).then((res) => {
		siteNameOptions.value = res.data.records.map((d) => {
			return {
				label: d.name,
				value: d.id,
			};
		});
	});
};
const handleChangeCompany = (e) => {
	const item = siteNameOptions.value.find((d) => d.value === e);
	form.applicationEnterprise = item.label;
};
const handleChangeType = (e) => {
	if (e === 20) {
		form.applicationEnterprise = null;
		form.applicationEnterpriseId = null;
	}
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
